<?php namespace Home\Controller; 
use Hdphp\Controller\Controller;
/**
 * 前台首页控制器
 * @author 武斌  <wubin.mail@foxmail.com>
 */
class IndexController extends CommonController{
	
	//构造函数
	public function __init()
	{
		
	}
	
    //动作
    public function index(){
		//公共头部注入变量
		$pageConf = array(
			'css'=>'index',
			'js'=>'index',
			'title'=>'优购时尚商城-时尚服饰鞋包网购首选-优生活，购时尚！',
		);
		View::with('pageConf',$pageConf);
    	//载入模板（TEMPLATE常量在前台入口index.php处定义）
       View::make(TEMPLATE . 'index.html');
    }
	/**
	 * 异步修改购物车
	 */
	public function edit()
	{
		if(!IS_AJAX) $this->error('非法请求');
		//post接受sid，数量
		$sid = Q('post.sid');
		$shuliang = Q('post.shuliang',0,'intaval');
		//组合要修改的数据
		$data=array(
		'sid'=>$sid,// 唯一 sid，添加购物车时自动生成 
		'num'=>$shuliang
		); 
		//更新购物车
		Cart::update($data);
		//修改总条数
		$total_rows = $_SESSION['cart']['total_rows'];
		View::ajax($total_rows);		
	}
	/**
	 * 删除购物车一条
	 */
	public function delOneCart()
	{
		if(!IS_AJAX) $this->error('非法请求');
		//post接受sid，商品小计，商品数量
	 	$sid = Q('post.sid',0,'intaval');
		$xiaoji = Q('post.xiaoji',0,'intaval');
		$shuliang = Q('post.shuliang',0,'intaval');
		//将对应该条删掉
		unset($_SESSION['cart']['goods'][$sid]);
		//如果购物车中商品数为0.将购物车删除
		if(count($_SESSION['cart']['goods'])==0){
			unset($_SESSION['cart']);
			View::ajax(0);
		}else{
			//修改购物车个总条数，中价
			$_SESSION['cart']['total_rows']=$_SESSION['cart']['total_rows']-$shuliang;
			$_SESSION['cart']['total']=$_SESSION['cart']['total']-$xiaoji;
			$total_rows = $_SESSION['cart']['total_rows'];
			View::ajax($total_rows);
		}					
	}
}
